<?php
  class widgets extends admin{
    function make_extra($buff,&$buff){
      if(isset($_GET['delid'])){
	kleowidgets::remove_widget((int)$_GET['delid']);
      }
      if(isset($_GET['pid'])){
	$order_pid = 'AND pid = '.(int)$_GET['pid'];
	$pid = (int)$_GET['pid'];
      } else {
	$order_pid = "";
	$pid = 0;
      }
      $query = $this->query("SELECT * FROM ".KLEO_TABLE_PREFIX."widgets, ".KLEO_TABLE_PREFIX."widgets_groups WHERE widgetid = wid $order_pid ORDER BY oid ASC");
      $i=0;
      if($this->num_rows($query) !== 0){
	$GLOBALS['has_widgets'] = true;
	while($row = $this->fetch_array($query)){
	  $row['html'] = htmlentities($row['html']);
	  $GLOBALS['widgets'][$i] = $row;
	  $GLOBALS['widgets'][$i]['position'] = $this->widget_places[$row['pid']];
	  if(empty($row['html'])){
	    $GLOBALS['widgets'][$i]['html'] = " ";
	  }
	  if(!empty($row['script'])){
	    $cut = explode("->", $row['script']);
	    $GLOBALS['widgets'][$i]['file'] = $cut[0].'/'.$cut[1].'.php';
	  } else {
	    $GLOBALS['widgets'][$i]['file'] = '<small>('.L_NONE.')</small>';
	  }
	    $GLOBALS['widgets'][$i]['sections'] = str_replace("*", "<small>(".L_ALL_SECTIONS.")</small>", $row['sections']);
	  $i++;
	}
      } else {
	$GLOBALS['has_widgets'] = false;
      }
      $this->tpl_set(array(
	"FILTER" => $this->pid_select()
	), false, $buff, $buff);
    }
    function pid_select(){
	$buff = '<select name="pid"  onchange="$(\'filter\').submit();">';
	foreach($this->widget_places as $value => $name){
	  if(!isset($pid)){
		$pid = false;
	  }
	  if(!isset($selected)){
		$selected = false;
	  }
	  if((int)$pid == $value){
	    $selected = 'selected';
	  }
	  $buff .= '<option value="'.$value.'" '.$selected.'>'.$name.'</option>';
	  unset($selected);
	}
	$buff .= '</select>';
	return $buff;
    }
  }
?>
